package com.timedomain.him.remake.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.timedomain.him.remake.models.ChatMessage;
import com.timedomain.him.remake.models.MorningCall;
import com.timedomain.him.remake.ui.common.CallingActivity;
import com.timedomain.him.remake.utils.AppSettings;
import com.timedomain.him.remake.utils.DB;
import com.timedomain.him.remake.utils.FileUtils;
import com.timedomain.him.remake.utils.HLog;
import com.timedomain.him.remake.utils.MorningCallManager;
import com.timedomain.him.remake.utils.TimeUtils;

public class MorningCallAlarmReceiver extends BroadcastReceiver {



    @Override
    public void onReceive(Context context, Intent intent) {

        if(AppSettings.morningCallDateStamp == TimeUtils.toDateStamp(TimeUtils.getTimeStampIgnoreTimeZone())){
            HLog.e("MorningCallReceiver", "onReceive: Duplicate call to MorningCallReceive");
            return;
        }

        AppSettings.morningCallDateStamp = TimeUtils.toDateStamp(TimeUtils.getTimeStampIgnoreTimeZone());
        AppSettings.nightChat = false;
        AppSettings.nightChatRequiredMorningCall = false;
        AppSettings.saveSettings(context);
        String oneCall = MorningCallManager.pullOneNewMorningCall(context);
        String path = oneCall;
        DB.MorningCall.Add(MorningCall.createForPath(context,path));

        ChatMessage cm = new ChatMessage(
                TimeUtils.getTimeStampIgnoreTimeZone(),
                FileUtils.getFileNameWithoutExtension(path),
                ChatMessage.TYPE_TIME,
                path,
                ""
        );
        DB.ChatMessage.Add(cm);
        cm = new ChatMessage(
                TimeUtils.getTimeStampIgnoreTimeZone(),
                FileUtils.getFileNameWithoutExtension(path),
                ChatMessage.TYPE_PHONE_CALL,
                path,
                ""
                );
        DB.ChatMessage.Add(cm);

        CallingActivity.startNewCall(context, path,true);
        MorningCallManager.setNextMorningCall(context);
    }
}
